import com.itheima.studentsystem.StudentMain;
import com.itheima.user.User;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        // 定义用户集合
        ArrayList<User> list = new ArrayList<>();
        // ctr+alt+t  弹出if else while 等选择
      /*  User user = new User("admin", "123456", "1111111111111111", "1571265641");
        list.add(user);*/

        loop:
        while (true) {
            System.out.println("欢迎使用学生管理系统");
            System.out.println("请选择操作项:");
            System.out.println("1:登录");
            System.out.println("2:注册");
            System.out.println("3:忘记密码");
            System.out.println("4:退出");

            Scanner scanner = new Scanner(System.in);
            String choose = scanner.next();
            switch (choose) {
                case "1":
                    login(list);
                    break;
                case "2":
                    register(list);
                    break;
                case "3":
                    forgetPassword(list);
                    break;
                case "4":
                    System.out.println("退出成功");
                    // System.exit(0); //结束程序
                    break loop;
                default:
                    System.out.println("没有该选择,请重新选择。");
            }
        }
    }

    // 登录
    public static void login(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        int count = 3;
        while (true) {
            // 验证用户名 是否注册
            System.out.println("请输入用户名:");
            String username = scanner.next();
            int index = getIndex(list, username);
            if (index == -1) {
                System.out.println("用户名未注册,请先注册");
                System.exit(0);
            }

            System.out.println("请输入密码:");
            String password = scanner.next();

            // 校验验证码
            String inputCaptcha;
            while (true) {
                // 获取验证码
                String captcha = createCaptcha();
                System.out.println("验证码：" + captcha);
                System.out.println("请输入验证码:");
                inputCaptcha = scanner.next();// 输入的验证码
                if (captcha.equalsIgnoreCase(inputCaptcha)) {// 忽略大小写
                    break;
                } else {
                    System.out.println("验证码错误，请重新输入");
                }
            }

            // 校验用户名和密码
            User user = list.get(index);
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("登录成功");

                //进入学生管理系统界面
                StudentMain.StudentSystem();

                break;
            } else {
                count--;
                if (count == 0) {
                    System.out.println("当前账号" + username + "已被锁定，请联系管理员0000-122222");
                    break;
                } else {
                    System.out.println("用户名或密码有误,请重新输入,还剩余" + count + "次机会");
                }
            }
        }

    }

    // 注册
    public static void register(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        // 验证用户名唯一
        String username;
        while (true) {
            System.out.println("请输入用户名:");
            username = scanner.next();

            // 判断用户名长度必须在3~15位之间
            int length = username.length();
            if (length >= 3 && length <= 15) {
                // 判断只能是字母加数字的组合，不能是纯数字
                boolean isPureNumber = checkUsername(username);
                if (!isPureNumber) {
                    System.out.println("只能是字母加数字的组合，不能是纯数字或符号，请重新输入");
                    continue;
                }

                boolean flag = contains(list, username);
                if (flag) {// 已存在
                    System.out.println("用户名已存在,请重新输入.");
                } else {
                    user.setUsername(username);
                    break;
                }
            } else {
                System.out.println("用户名长度必须在3~15位之间，请重新输入");
            }

        }

        String password;
        // 密码验证
        while (true) {
            System.out.println("请输入密码:");
            password = scanner.next();
            System.out.println("请再次输入密码:");
            String passwordAgain = scanner.next();
            if (password.equals(passwordAgain)) {
                user.setPassword(password);
                break;
            }
            System.out.println("两次输入的密码不一致");
        }

        // 验证身份证号

        String identity;
        while (true) {
            System.out.println("请输入身份证号码:");
            identity = scanner.next();
            if (identity.length() < 18) {
                System.out.println("身份证号码不满足18位，请重新输入");
                continue;
            }

            if (identity.length() > 18) {
                System.out.println("身份证号码不能大于18位，请重新输入");
                continue;
            }

            if (identity.charAt(0) == '0') {
                System.out.println("身份证号码不能以0开头，请重新输入");
                continue;
            }

            if (!checkPureNumber(identity.substring(0, 17))) {
                System.out.println("身份证号码前17位必须是纯数字，请重新输入");
                continue;
            }

            // 校验最后一位 是不是数字或X或x
            char lastChar = identity.charAt(17);
            if ((lastChar >= '0' && lastChar <= '9') || lastChar == 'X' || lastChar == 'x') {
                user.setIDCardNumber(identity);
                break;
            }
            System.out.println("身份证号码最后一位必须是数字或大小写的x，请重新输入");
        }


        // 验证手机号
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号:");
            phoneNumber = scanner.next();
            // 判断手机号长度11位
            if (phoneNumber.length() != 11) {
                System.out.println("手机号只能是11位，请重新输入");
                continue;
            }

            if (phoneNumber.charAt(0) == '0') {
                System.out.println("手机号不能以0开头，请重新输入");
                continue;
            }

            if (!checkPureNumber(phoneNumber)) {
                System.out.println("手机号必须是数字，请重新输入");
                continue;
            }
            user.setPhoneNumber(phoneNumber);
            break;

        }


        list.add(user);
        System.out.println("恭喜您,注册成功");

    }

    // 忘记密码
    public static void forgetPassword(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名:");

        String username = scanner.next();
        int index = getIndex(list, username);
        if (index == -1) {
            System.out.println("未注册");
            System.exit(0);
        }

        System.out.println("请输入身份证号:");
        String idCardNumber = scanner.next();

        System.out.println("请输入手机号:");
        String phoneNumber = scanner.next();

        User user = list.get(index);

        if (!(user.getIDCardNumber().equals(idCardNumber) && user.getPhoneNumber().equals(phoneNumber))) {
            System.out.println("账号信息不匹配，修改失败");
        }
        while (true){
            System.out.println("请输入新密码:");
            String password = scanner.next();

            System.out.println("请再次输入新密码:");
            String newPassword = scanner.next();
            if (!password.equals(newPassword)){
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }
            user.setPassword(password);
            System.out.println("修改成功");
            break;
        }

    }

    // 校验是否是纯数字
    public static boolean checkPureNumber(String number) {
        for (int i = 0; i < number.length(); i++) {
            char c = number.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    // 校验用户名组合字母加数字
    public static boolean checkUsername(String username) {
        int letterCount = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                letterCount++;
                break;
            }
        }

        return letterCount != 0;
    }


    // 查询用户是否已经存在
    public static boolean contains(ArrayList<User> list, String username) {
        return getIndex(list, username) >= 0;
    }

    // 根据用户名获取索引
    public static int getIndex(ArrayList<User> list, String username) {

        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(username)) {
                return i;
            }
        }

        return -1;
    }


    // 生成验证码
    // 长度为5位
    // 由4位大写或小写字符和1位数字组成，同一个字母可重复
    // 数字可以出现在任意位置
    public static String createCaptcha() {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        // 生成大小字母集合
        ArrayList<Character> letterList = new ArrayList<>();

        for (int i = 0; i < 26; i++) {
            char C = (char) (i + 65);
            letterList.add(C);
            char c = (char) (i + 97);
            letterList.add(c);
        }

        // 随机获取4个字母
        for (int i = 0; i < 4; i++) {
            int letterIndex = random.nextInt(letterList.size());
            char letter = letterList.get(letterIndex);
            result.append(letter);
        }

        // 生成数字
        int number = random.nextInt(10);

        // 随机数字索引
        int randomIndex = random.nextInt(5);
        result.insert(randomIndex, number);


        return result.toString();
    }
}
